package com.example.brazil_project.util;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.framework.Assert;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;



import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;

import android.util.Log;



public class HttpUtil {
	//private static HttpParams httpParams;  
	  
    
	 public static String doPost(String url, List<NameValuePair> params)  { 
		 String result=null;
		 try {
			HttpEntity entity = new UrlEncodedFormEntity(params,HTTP.UTF_8);
		 	HttpClient client = new DefaultHttpClient();  
	        HttpPost post = new HttpPost(url);  
//	       
//	        params.add(new BasicNameValuePair("id", "1001"));  
//	        params.add(new BasicNameValuePair("name", "john"));  
//	        params.add(new BasicNameValuePair("age", "60"));  
	       /* HttpEntity formEntity;
			
				formEntity = new UrlEncodedFormEntity(params);*/
			 
	        post.setEntity(entity);  
	        HttpResponse response = client.execute(post);  
	        

	        client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);
	        
	        
	        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
	            InputStream is = response.getEntity().getContent();  
	            result = inStream2String(is); 
	            
	            
	           // Assert.assertEquals(result, "POST_SUCCESS");  
	        } else{
	        	 result = "无法连接服务器，请检查您的网络"; 
	        	
	        }
	        } catch (UnsupportedEncodingException e) {
	        	 result = "无法连接服务器，请检查您的网络";
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				 result = "无法连接服务器，请检查您的网络";
				e.printStackTrace();
			} catch (IOException e) {
				 result = "无法连接服务器，请检查您的网络";
				e.printStackTrace();
			} catch (Exception e) {
				 result = "无法连接服务器，请检查您的网络";
				e.printStackTrace();
			} 
		 if(!StringUtils.isEmpty(result)&&result.lastIndexOf("{")<0){
			 result =result.replace("\"", "");
			
//			 result =result.replace("}{", "},{");
			}
		 result =result.replace("'", "‘");
		 result =result.replace("<", "&lt;");
		 result =result.replace(">", "&gt;");
			return result;     
	    }  
	  
	//将输入流转换成字符串  
	    private static String inStream2String(InputStream is) throws Exception {  
	        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
	        byte[] buf = new byte[1024];  
	        int len = -1;  
	        while ((len = is.read(buf)) != -1) {  
	            baos.write(buf, 0, len);  
	        }  
	        return new String(baos.toByteArray());  
	    }  
	    
	    
	    public static String doPost(String url) {
	    	
	    	List<NameValuePair> params = new ArrayList<NameValuePair>();  
	        params.add(new BasicNameValuePair("ISISISIS", "TESTEST"));  
	    	String result = doPost( url,params);
			return result; 
	    	
	    	
	    	
	    }
	    
	    //用map传参数
   public static String doPost(String url,Map map1) {
	   List<NameValuePair> params = new ArrayList<NameValuePair>();  
       params.add(new BasicNameValuePair("ISISISIS", "TESTEST"));  
	   if(!map1.isEmpty()){
	         for(Iterator iter = map1.entrySet().iterator();iter.hasNext();){ 
	        	 Map.Entry element = (Map.Entry)iter.next(); 
	        	 String strKey = element.getKey().toString();
	        	 String value = map1.get(strKey)+"";
	        	 if(value == null ||value.equals("null")){
	        		 continue;
	        		 
	        	 }
	        	  params.add(new BasicNameValuePair(strKey,value));
	        	  System.out.println(value);
	        	 } 
      }
	    	
	    	String result = doPost( url,params);
			return result; 
	    	
	    	
	    	
	    }
	    
	public static void main(String[] args) {
	    List<NameValuePair> params = new ArrayList<NameValuePair>();  
        params.add(new BasicNameValuePair("email", "firewings.r@gmail.com"));  
        params.add(new BasicNameValuePair("password", "954619"));  
        params.add(new BasicNameValuePair("remember", "1"));  
        params.add(new BasicNameValuePair("from", "kx"));  
        params.add(new BasicNameValuePair("login", "登 录"));  
        params.add(new BasicNameValuePair("refcode", ""));  
        params.add(new BasicNameValuePair("refuid", "0"));  
   
	  
	        String url2 = "http://www.google.cn/search";  
	  
	        String url = "http://wap.kaixin001.com/home/";  
	  
	        doPost(url, params);
	}
    }
